package com.wulwu.caffeine.service.impl;

import com.wulwu.caffeine.utils.CacheUtil;
import com.wulwu.caffeine.entity.OrderActionCode;
import com.wulwu.caffeine.entity.OrderActionCodeExample;
import com.wulwu.caffeine.mapper.OrderActionCodeMapper;
import com.wulwu.caffeine.service.OrderCacheService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentMap;

@Service
public class OrderCacheServiceImpl implements OrderCacheService {

    @Autowired
    private OrderActionCodeMapper orderActionCodeMapper;

    @Override
    public OrderActionCode queryActionCode(Integer actionCode) {
        //手动填充 --> 同步填充
        try {
            //已并发压测 500线程,这种写法 == LocalCache
            Object value = CacheUtil.cacheManager.get(actionCode.toString(), k -> createExpensiveGraph(k));
            return (OrderActionCode) value;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<OrderActionCode> initActionCodeCache() {
        List<OrderActionCode> orderActionCodes = orderActionCodeMapper.selectByExample(null);
        if (Objects.nonNull(orderActionCodes) && orderActionCodes.size() > 0) {
            for (OrderActionCode actionCode: orderActionCodes) {
                CacheUtil.setValue(actionCode.getActionCode().toString(),actionCode);
            }
        }
        return orderActionCodes;
    }

    @Override
    public List<OrderActionCode> queryAllActionCode() {
        ConcurrentMap<@NonNull String, @NonNull Object> concurrentMap = CacheUtil.cacheManager.asMap();
        List<OrderActionCode> actionCodeList = new ArrayList<>();
        for (Map.Entry<@NonNull String, @NonNull Object> entries : concurrentMap.entrySet())  {
            actionCodeList.add((OrderActionCode) entries.getValue());
        }
        if (CollectionUtils.isEmpty(actionCodeList)){
            actionCodeList = initActionCodeCache();
        }
        return actionCodeList;
    }

    @Override
    public Map<String, Object> actionCodeCache() {
        ConcurrentMap<@NonNull String, @NonNull Object> concurrentMap = CacheUtil.getAllCache();
        return concurrentMap;
    }

    @Override
    public void clearActionCodeCache() {
        CacheUtil.delAllValue();
    }

    @Override
    public void batchClearActionCodeCache(List<String> keys) {
        CacheUtil.delBatchValue(keys);
    }

    @Override
    public void delActionCodeCache(String key) {
        CacheUtil.delValue(key);
    }

    private Object createExpensiveGraph(String key) {
        System.out.println("----------查询 db-------->" + key);
        if (StringUtils.isNotBlank(key)) {
            OrderActionCodeExample example = new OrderActionCodeExample();
            example.createCriteria().andActionCodeEqualTo(Integer.parseInt(key));
            List<OrderActionCode> orderActionCodes = orderActionCodeMapper.selectByExample(example);
            if (Objects.nonNull(orderActionCodes) && orderActionCodes.size() > 0) {
                return orderActionCodes.get(0);
            }
        }
        return null;
    }



}
